<html>
<body>

<h2>Simple Calculator</h2>

<form action="calculator.php" method="get">
<input type="text" name="reg_expr">
<input type="submit" value="Calculator" />
</form>

<h3>Result:</h3>

<?php 

$input_string = $_GET["reg_expr"];

try {

## at least one 0 denomerator => don't print anything
if (preg_match("#[0-9]/0+#", $input_string) >= 1) {

throw new Exception("$input_string = ");

## at least one character || first character is * or / || any whitespace
} elseif (preg_match("#([^0-9+-/*]|[+-/*]{1,}$|^[*/])#", $input_string) >= 1) {

throw new Exception("Invalid input expression $input_string.");

} else {

    if (strlen($input_string) != 0) {
      echo "$input_string = ", eval("return $input_string;");
    }
}

} catch (Exception $e) {
    echo $e->getMessage();
}

?>

<html>
<body>

